// AseContainer.h: interface for the AseContainer class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BINCONTAINER_H__A7F47673_F022_4295_93E9_C8677734E830__INCLUDED_)
#define AFX_BINCONTAINER_H__A7F47673_F022_4295_93E9_C8677734E830__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma warning(disable : 4996)

enum
{
	PCMESH,
	NPCMESH
};
enum
{
	MESHVERSION_WEAPON = 10,
};

struct BINVERTEX
{
	float x;
	float y;	
	float z;
	float nx;
	float ny;
	float nz;
	float tu;
	float tv;
};
struct BINMESHVERT
{
	BINVERTEX vert;
	int nbone;
	BYTE boneid[4];
	float weight[4];
	BYTE flag;
	BINMESHVERT():
	nbone(1),
	flag(0)
	{
	}
	void Addboneid(int id)
	{
		for (int i=0;i<nbone;i++)if (boneid[i]==id)return;
		boneid[nbone]=id;
		nbone++;
		for (int i=0;i<nbone;i++)weight[i]=1.0f/(float)nbone;
	}
};
#define MAX_MODELVERT 6000
#define MAX_MODELINDEX 6000
#define MAX_MODELATTR 6000
struct BINMESH
{
	int nvert;
	int nface;
	int maxbone;
	BINMESHVERT mesh[MAX_MODELVERT];
	D3DXBONECOMBINATION pBoneComp[10];
	D3DXMATRIX fcloth;
	D3DXMATRIX bcloth;
	int cattr;
	WORD index[MAX_MODELINDEX];
	DWORD matid[MAX_MODELATTR];
	BINMESH():
	cattr(0)
	{
	}
	~BINMESH()
	{
	}
};
struct SPositionKeyBIN
{
	DWORD dwTime;
	D3DXVECTOR3 vPos;
};
struct SRotateKeyBIN
{
	DWORD dwTime;
	D3DXQUATERNION quat;
};
struct _BONEBIN
{
	D3DXMATRIXA16 TM;
	D3DXMATRIXA16 offsetTM;
	char bonename[50];
	char parbonename[50];
	char upordown;
	BYTE boneid;
	int npos;
	int nrot;
	int refchild;
	int nchild;
	_BONEBIN *child[15];
	_BONEBIN *parent;
    _BONEBIN()
        :
	nchild(0),
	npos(0),
	nrot(0),
	refchild(0),
	upordown(1),
	parent(NULL)
	{
		for (int i=0;i<15;i++)child[i]=NULL;
		memset(bonename,0,50);
		memset(parbonename,0,50);
	}
	~_BONEBIN()
	{
//		if (npos)delete pos;
//		if (nrot)delete rot;
	}
	SPositionKeyBIN *pos;
	SRotateKeyBIN *rot;
};
struct _BONELIST
{
	char bonename[256];
	_BONEBIN **firstbone;
	int boneid;
	int nbone;
	_BONELIST *next;
    _BONELIST()
        :
	next(0)
	{
	}
	~_BONELIST()
	{
	}
};
#ifndef BLURSTEP 
#define BLURSTEP 25
#endif
extern char KindTable[];
extern char ItemTable[];
extern char SexTable[];

class BinContainer  
{
public:
	void SaveSkinItem(FILE *fp,D3DXMATRIX &mat);
	void SaveBone(_BONELIST *blist);
	D3DXVECTOR3 pMin,pMax;
	void SetBoneTree(_BONELIST *bone);
	_BONELIST * GetBone(int boneid);
	BinContainer();
	virtual ~BinContainer();
	BINMESH mymesh;

	_BONELIST *m_Bonelist;

	_BONEBIN **firstbone;
	_BONEBIN *curbone;
	_BONEBIN *headbone;

	int num_bone;
	int nmaterial;
	D3DXMATERIAL material[6];


	void CombineMesh();
	void ResetIndex();
	int GetFrameTMnNAME(void *_pframe,char **str);
	void GetAdjacency(LPD3DXBUFFER *buff2);
	void GetBoneNames(LPD3DXBUFFER *buff2);
	void GetBoneMatrix(LPD3DXBUFFER *buff2);
	void GetSkinMesh(char *name, BYTE ver);
	void GetSkinMeshSW(DWORD option,DWORD fvf,LPDIRECT3DDEVICE9 pDevice,VOID *mesh,LPD3DXBUFFER indice);
	int GetBoneIndex(char *str);
	_BONELIST* LoadBone(const char *str,int boneid);
	int GetNextBone();
//	int LoadAnimation(char *str);
	void GetTexture(DWORD &cmat, LPD3DXBUFFER *buff2);
	void ReadMaterial();
	void LoadMesh(BYTE *file,int part,int kind);
private:
	char OpenedName[6][80];
};

#endif // !defined(AFX_ASECONTAINER_H__A7F47673_F022_4295_93E9_C8677734E830__INCLUDED_)
